﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Blam_Unity.Classes.X360
{
    static class readContainerHeaderData
    {
        /// <summary>
        /// Read all the relevant data stored in the CON header of the film
        /// </summary>
        /// <param name="mio">Mojos IO already declared</param>
        /// <returns></returns>
        public static string[] readFilmHeader(MojosIO mio)
        {
            string[] filmInfo = new string[7];
                
            //Get Film Name
            mio.Position = 0xD0C1;
            filmInfo[0] = mio.ReadString(0x8E, Encoding.ASCII);
            filmInfo[0] = filmInfo[0].TrimEnd(' ');

            //Get Film Author
            mio.Position = 0xD088;
            filmInfo[1] = mio.ReadString(16, Encoding.ASCII);
            filmInfo[1] = filmInfo[1].TrimEnd(' ');

            //Get Film Desc
            mio.Position = 0xD1C1;
            filmInfo[2] = mio.ReadString(0xF0, Encoding.Unicode);
            filmInfo[2] = filmInfo[2].TrimEnd(' ');

            //Get GameType Name
            mio.Position = 0xD999;
            filmInfo[3] = mio.ReadString(0x90, Encoding.Unicode);
            filmInfo[3] = filmInfo[3].TrimEnd(' ');

            //Get GameType Desc
            mio.Position = 0xDA99;
            filmInfo[4] = mio.ReadString(0x100, Encoding.Unicode);
            filmInfo[4] = filmInfo[4].TrimEnd(' ');

            //Get MapName
            mio.Position = 0x1D11C;
            filmInfo[5] = mio.ReadString(0x103, Encoding.ASCII);
            filmInfo[5] = filmInfo[5].TrimEnd(' ');

            //Get Rally Point
            mio.Position = 0x1D220;
            filmInfo[6] = mio.ReadString(0x88, Encoding.ASCII);
            filmInfo[6] = filmInfo[6].TrimEnd(' ');

            return filmInfo;
        }
    }
        
    static class saveContainerHeaderData
    {
        /// <summary>
        /// Save content to the CON header of the film
        /// </summary>
        /// <param name="mio">Mojos IO already declared</param>
        /// <param name="filmName">The Name of the Film</param>
        /// <param name="filmDesc">The Description of the Film</param>
        /// <param name="filmAuthor">The Xbox 360 Gamertag of the Author of the Film</param>
        /// <param name="gametypeName">The Name of the Gametype</param>
        /// <param name="gametypeDesc">The Description of the Gametype</param>
        /// <param name="map">The engine path to the map used</param>
        /// <param name="rallyPoint">The rallypoint engine call used</param>
        /// <param name="isCampaign">Check if the film is in the campaign/firefight</param>
        public static void saveFilmheader(MojosIO mio, string filmName, string filmDesc, string filmAuthor, string gametypeName, string gametypeDesc, string map, string rallyPoint, bool isCampaign)
        {
            //Write Film Name
            mio.Position = 0x412;
            mio.WriteString("                                                                                 ", Encoding.Unicode);
            mio.Position = 0x412;
            mio.WriteString(filmName, Encoding.Unicode);
            mio.Position = 0xD0C1;
            mio.WriteString("                                                                                 ", Encoding.Unicode);
            mio.Position = 0xD0C1;
            mio.WriteString(filmName, Encoding.Unicode);

            //Write Film Desc
            mio.Position = 0xD1C1;
            mio.WriteString("                                                                                                                        ", Encoding.Unicode);
            mio.Position = 0xD1C1;
            mio.WriteString(filmDesc, Encoding.Unicode);

            //Write Film Author
            mio.Position = 0xD088;
            mio.WriteString("                ", Encoding.ASCII);
            mio.Position = 0xD088;
            mio.WriteString(filmAuthor, Encoding.ASCII);
            mio.Position = 0xD0AC;
            mio.WriteString("                ", Encoding.ASCII);
            mio.Position = 0xD0AC;
            mio.WriteString(filmAuthor, Encoding.ASCII);

            //Write Gametype Name
            mio.Position = 0xD999;
            mio.WriteString("                                                                                 ", Encoding.Unicode);
            mio.Position = 0xD999;
            mio.WriteString(gametypeName, Encoding.Unicode);

            //Write Gametype Desc
            mio.Position = 0xDA99;
            mio.WriteString("                                                                                                                        ", Encoding.Unicode);
            mio.Position = 0xDA99;
            mio.WriteString(gametypeDesc, Encoding.Unicode);

            //Write Map Name
            mio.Position = 0x1D11C;
            mio.WriteString("                                                                                 ", Encoding.ASCII);
            mio.Position = 0x1D11C;
            mio.WriteString(map, Encoding.ASCII);

            if (isCampaign == true)
            {
                //Write Rally Point
                mio.Position = 0x1D220;
                mio.WriteString("                                                                                 ", Encoding.ASCII);
                mio.Position = 0x1D220;
                mio.WriteString(rallyPoint, Encoding.ASCII);
            }
            else { }
        }
    }
}